# TABLES MAIN TEXT

# produces Tables 1 and 2 displayed in the main text


# Session info
# R version 4.1.0 (2021-05-18)
# Platform: x86_64-apple-darwin17.0 (64-bit)
# Running under: macOS 13.1


# Load packages -----------------------------------------------------------
# if needed install packages first:
#install.packages(c("tidyverse", "stargazer"))

library(tidyverse)
library(stargazer)

# Load data ---------------------------------------------------------------

rm(list = ls())

# load report-level data
data_rep_level <- read_csv("data/data_rep_level.csv")

meta_pred_un <- read_csv("data/meta_pred_un.csv")

prog_proj <- meta_pred_un %>% 
  ## Filter UN data for program and project evaluation types only
  filter(eval_type != "Institutional",
       eval_type != "Thematic") %>% 
  # standardize evaluation type
  mutate(eval_type = ifelse(eval_type == "Programme", "Program", eval_type),
         eval_type = factor(eval_type))


##########################

#### Table 1

##########################


# IV (IEG Rating) alone, as continuous variable 1-6
model_iv_num <- lm(sentiment ~ IEG_Outcome_num,
                   data_rep_level)

# IV and control for report type
model_iv_num_type <- lm(sentiment ~ IEG_Outcome_num + 
                          factor(type),
                        data_rep_level)

# IV, report type and country and year fixed affects
model_iv_num_type_fe <- lm(sentiment ~ IEG_Outcome_num + 
                             factor(type) +
                             factor(country) +
                             factor(year),
                           data_rep_level)


## Table 1 - Output for main text models
model_output_num <- list(model_iv_num, model_iv_num_type, model_iv_num_type_fe)

stargazer(model_output_num, type = "html", 
          out = "output/table1.htm", 
          keep.stat = c("n", "rsq", "adj.rsq"), 
          title = "Model Summary", 
          omit = c("country", "year"), ci = T,
          intercept.bottom =T,
          dep.var.labels = "Positive Assessment Share",
          covariate.labels = c("IEG Rating (numeric)",
                               "Report Type (PPAR)"
          ),
          add.lines=list(c('Year FE', 'No','No', 'Yes'), 
                         c('Country FE', 'No','No', 'Yes')),
          column.sep.width = "50pt"
)



##########################

#### Table 2

##########################


#### Models for program/project comparison

## positive assessment share and evaluation type only
model_prog_proj <- lm(sentiment ~ factor(eval_type),
                      prog_proj)

## positive assessment share and evaluation type with fixed effects
model_prog_proj_fe <- lm(sentiment ~ factor(eval_type) + 
                           factor(IO) +
                           factor(eval_level) +
                           factor(year),
                         prog_proj)


## Model output for evaluation type models
types <- list(model_prog_proj, model_prog_proj_fe)

stargazer(types, type = "html", 
          out = "output/table2.htm", 
          keep.stat = c("n", "rsq", "adj.rsq"), 
          title = "Model Summary", 
          omit = c("eval_level", "year", 'IO'), ci = T,
          dep.var.labels = "Positive Assessment Share",
          intercept.bottom = T,
          covariate.labels = c('Evaluation Type (Project)'),
          add.lines=list(c('Year FE', 'No', 'Yes'), 
                         c('Country FE', 'No','Yes'),
                         c('IO FE', 'No', 'Yes')),
          column.sep.width = "50pt"
)




